package com.lanou.mynews.video;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.mynews.R;


import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class MyVideoFragment extends Fragment {
    private ListView listView;
    private VideoAdapter adapter;
    private RequestQueue requestQueue;
    private List<VideoEty.VideoListEntity> datas;
    private String getUrl = "http://c.3g.163.com/nc/video/home/0-10.html";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.video_fragment, null);

        listView = (ListView) view.findViewById(R.id.video_lv);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(),VideoPlayerActivity.class);
                intent.putExtra("url",datas.get(i).getMp4_url());
                getActivity().startActivity(intent);
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = Volley.newRequestQueue(getActivity());// 初始化请求队列
        getData();
    }
    private void getData() {
        System.out.println(getUrl);
        StringRequest request = new StringRequest(getUrl,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {// 网址正确的时候会回调的方法
                        if (response != null) {// response是从网络返回的全部数据,字符串类型
                            Gson gson = new Gson();
                            // 将解析的字符串类型的值,转成TicketResult中的格式
                            VideoEty videoEty = gson.fromJson(response,
                                    VideoEty.class);
                            if (videoEty != null) {
                                datas = videoEty.getVideoList();

                                //绑定适配器
                                adapter = new VideoAdapter(getActivity(), datas);
                                listView.setAdapter(adapter);

                            }
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

            }
        });
        // 将图片请求对象加入到请求队列中
        requestQueue.add(request);
    }



}
